<?php
// +----------------------------------------------------------------------
// | RXThinkCMF敏捷开发框架 [ 赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2023 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | Licensed LGPL-3.0 RXThinkCMF并不是自由软件，未经许可禁止去掉相关版权
// +----------------------------------------------------------------------
// | 官方网站: https://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: @牧羊人 团队荣誉出品
// +----------------------------------------------------------------------
// | 版权和免责声明:
// | 本团队对该软件框架产品拥有知识产权（包括但不限于商标权、专利权、著作权、商业秘密等）
// | 均受到相关法律法规的保护，任何个人、组织和单位不得在未经本团队书面授权的情况下对所授权
// | 软件框架产品本身申请相关的知识产权，禁止用于任何违法、侵害他人合法权益等恶意的行为，禁
// | 止用于任何违反我国法律法规的一切项目研发，任何个人、组织和单位用于项目研发而产生的任何
// | 意外、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、
// | 附带或衍生的损失等)，本团队不承担任何法律责任，本软件框架禁止任何单位和个人、组织用于
// | 任何违法、侵害他人合法利益等恶意的行为，如有发现违规、违法的犯罪行为，本团队将无条件配
// | 合公安机关调查取证同时保留一切以法律手段起诉的权利，本软件框架只能用于公司和个人内部的
// | 法律所允许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\model;

use app\admin\service\RoleService;

/**
 * 学生-模型
 * @author 牧羊人
 * @since: 2024/06/25
 * Class Student
 * @package app\admin\model
 */
class Student extends BaseModel
{
    // 设置数据表名
    protected $name = "student";

    /**
     * 获取缓存信息
     * @param int $id 记录ID
     * @return \app\common\model\数据信息|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 牧羊人
     * @since: 2024/06/25
     */
    public function getInfo($id)
    {
        $info = parent::getInfo($id); // TODO: Change the autogenerated stub
        if ($info) {
        }
        return $info;
    }

    //左侧省市县街道学校 5级树状
    public function schooleMenu()
    {
        //获取用户的角色
        $userId = parent::$staticUserId;
        $uinfo = db('user')->where(['id' => $userId])->find();
        $role_ids = db('user_role')->where(['user_id' => $userId])->column('role_id');
        $special_role  = (new RoleService())->special_role; //[5, 6, 7, 8];市级管理员、县(区)级管理、街道办管理员、学校管理员
        $intersect_role = array_intersect($role_ids, $special_role);

        //如果是某一级管理员，只看权限内学校
        $schoole_list = db('schoole')->where(['mark' => 1])->select();
        $where = [
            'province_code' => array_column($schoole_list, 'province_code'),
            'city_code' => array_column($schoole_list, 'city_code'),
            'district_code' => array_column($schoole_list, 'district_code'),
            'streets_code' => array_column($schoole_list, 'streets_code'),
        ];
        if ($intersect_role) {
            $role_id = max($intersect_role);
            switch ($role_id) {
                case 5:
                    $where['province_code'] = $uinfo['province_code'];
                    $where['city_code'] = $uinfo['city_code'];
                    break;
                case 6:
                    $where['province_code'] = $uinfo['province_code'];
                    $where['city_code'] = $uinfo['city_code'];
                    $where['district_code'] = $uinfo['district_code'];
                    break;
                case 7:
                    $where['province_code'] = $uinfo['province_code'];
                    $where['city_code'] = $uinfo['city_code'];
                    $where['district_code'] = $uinfo['district_code'];
                    $where['streets_code'] = $uinfo['streets_code'];
                    break;
                case 8:
                    $where['province_code'] = $uinfo['province_code'];
                    $where['city_code'] = $uinfo['city_code'];
                    $where['district_code'] = $uinfo['district_code'];
                    $where['streets_code'] = $uinfo['streets_code'];
                    $where['schoole_id'] = $uinfo['schoole_id'];
                    break;
            }
        }

        $list = $this->getChilds($where);
        return message("操作成功", true, $list);
    }

    //$level 层级
    private function getChilds($where, $level = 1, $pid = 0)
    {
        $map = [];
        $map[] = ['level', '=', $level];
        $map[] = ['pid', '=', $pid];
        if ($level == 1) $map[] = ['citycode', (is_array($where['province_code']) ? 'in' : '='), $where['province_code']];
        if ($level == 2) $map[] = ['citycode', (is_array($where['city_code']) ? 'in' : '='), $where['city_code']];
        if ($level == 3) $map[] = ['citycode', (is_array($where['district_code']) ? 'in' : '='), $where['district_code']];

        if ($level < 4) {
            $list = db('city')->field('id,pid,level,name,citycode')->where($map)->order("sort asc")->select();
            if (!empty($list)) {
                foreach ($list as &$val) {
                    $nextLevel = $level + 1;
                    $pid = ($nextLevel == 4) ? $val['citycode'] : $val['id'];
                    $val['children'] = $this->getChilds($where, $nextLevel, $pid);
                }
            }
            return $list;
        }

        if ($level == 4) {
            $map = [];
            $map[] = ['pid', '=', $pid];
            $map[] = ['id', (is_array($where['streets_code']) ? 'in' : '='), $where['streets_code']];
            $list = db('city_level4')->field('id,pid,name')->where($map)->select();
            if (!empty($list)) {
                foreach ($list as &$val) {
                    //学校
                    $map = [];
                    $map[] = ['mark', '=', 1];
                    $map[] = ['streets_code', '=', $val['id']];
                    if (isset($where['schoole_id'])) $map[] = ['id', '=', $where['schoole_id']];
                    $val['children'] = db('schoole')->field('id,streets_code as pid,title as name')->where($map)->select();
                }
            }
            return $list;
        }

        return [];
    }

    public function getGradeTitleAttr()
    {
        return generate_grade($this->grade_id);
    }

    public function getClassTitleAttr()
    {
        return generate_class($this->class_id);
    }

    // 关联schoole表
    public function schoole()
    {
        return $this->hasOne(Schoole::class, 'id', 'schoole_id')->bind(['schoole_name' => 'title']);
    }

    public function work()
    {
        return $this->hasOne(StudentWorkLog::class, 'student_id', 'id');
    }
}
